package com.ay.selfservicemachine.permission;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.ay.selfservicemachine.base.HQWAppCompatActivity;
import com.ay.selfservicemachine.databinding.DialogPermissionSpecialBinding;
import com.ay.selfservicemachine.dialog.BaseDialog;


public class PermissionSpecialDialog extends BaseDialog<DialogPermissionSpecialBinding> implements View.OnClickListener {
    HQWAppCompatActivity hqwAppCompatActivity;
    String permission;
    boolean isConstraint;
    String content;
    String content2;
    PermissionListener permissionListener;

    int frequency;//验证次数

    public PermissionSpecialDialog(@NonNull HQWAppCompatActivity context, String permission, boolean isConstraint,
                                   String content, String content2, PermissionListener permissionListener) {
        super(context);
        this.hqwAppCompatActivity = context;
        this.permission = permission;
        this.isConstraint = isConstraint;
        this.content = content;
        this.content2 = content2;
        this.permissionListener = permissionListener;
        setCancelable(false);
        setBorderExtension();
    }

    @Override
    protected DialogPermissionSpecialBinding viewBinding() {
        return DialogPermissionSpecialBinding.inflate(getLayoutInflater());
    }


    @Override
    protected void initView() {
        super.initView();
        root.tvPermissionCancel.setOnClickListener(this);
        root.tvPermissionVerify.setOnClickListener(this);
        root.tvPermissionContent.setText(TextUtils.isEmpty(content) ? "" : content);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == root.tvPermissionCancel.getId()) {
            dismiss();
            if (permissionListener != null) {
                permissionListener.onfailure();
            }
        } else if (v.getId() == root.tvPermissionVerify.getId()) {//验证
            Verify();
        }
    }

    public void Verify() {
        hqwAppCompatActivity.HQWPermissionSpecial(permission, new HQWPermissionListener() {
            @Override
            public void onsucceed() {
                if (permissionListener != null) {
                    permissionListener.onsucceed();
                    dismiss();
                }
            }

            @Override
            public void onfailure() {
                if (isConstraint) {//是否进行2次请求
                    root.tvPermissionContent.setText(TextUtils.isEmpty(content2) ? "" : content2);
                    if (frequency >= 1) {
                        if (permissionListener != null) {
                            PermissionCommander.showPermissionSetting(getContext(), permissionListener, "");
                            dismiss();
                        }
                    }
                    frequency++;
                } else {
                    if (permissionListener != null) {
                        permissionListener.onfailure();
                    }
                }

            }
        });

    }


}

